package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.seu.herald.informationplatform.bean.league.Vote;
import cn.edu.seu.herald.informationplatform.dao.cookie.LeagueUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.SessionIdNotFound;
import cn.edu.seu.herald.informationplatform.dao.database.VoteDataAccess;

public class VoteTitleEdit extends HttpServlet {


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String voteName = request.getParameter("voteName");
	
		try {
			LeagueUserSessionCookieAccess luSessionCookieAccess= new LeagueUserSessionCookieAccess(request);
			VoteDataAccess voteDataAccess = VoteDataAccess.getInstance();
			Vote preVote = voteDataAccess.getVoteByLeagueId(luSessionCookieAccess.getLeagueId());
		   
			preVote.setTitle(voteName);
		    voteDataAccess.updateVote(preVote);
		    response.sendRedirect("./polling.jsp?league="+luSessionCookieAccess.getLeagueId());
		} catch (SessionIdNotFound e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
